// exports.AL_Framework_Sub_Game = void 0;
// var a = function () {};
// var s = (function () {
//     function e() {
//         this.common = new a();
//         this.map = {};
//     }
//     e.prototype.huoqu_mingzi = function () {
//         return this.curName;
//     };
//     e.prototype.shezhi_mingzi = function (e) {
//         this.curName = e;
//     };
//     e.prototype.shezhi_tongyong_gongneng = function (e) {
//         var t = this;
//         this.common = e;
//         var o = e.huidao_shouye;
//         if (o) {
//             this.common.huidao_shouye = function (i, n) {
//                 if (void 0 === i) {
//                     i = {};
//                 }
//                 if (void 0 === n) {
//                     n = "";
//                 }
//                 return (
//                     t.common.showLoading
//                         ? t.common.disableLoading
//                             ? Promise.resolve()
//                             : t.common.showLoading()
//                         : Promise.resolve()
//                 ).then(function () {
//                     t.common.gameToHomeName = n || t.curName;
//                     if (i.onShowLoadingEnd) {
//                         i.onShowLoadingEnd();
//                     }
//                     return o(i).then(function () {
//                         if (t.common.mainGroup) {
//                             ailin_sdk_manager.initPhysics(e.mainGroup);
//                         }
//                         if (i.onGoHomeEnd) {
//                             i.onGoHomeEnd();
//                         }
//                         return (
//                             t.common.hideLoading
//                                 ? t.common.disableLoading
//                                     ? Promise.resolve()
//                                     : t.common.hideLoading()
//                                 : Promise.resolve()
//                         ).then(function () {
//                             if (i.onHideLoadingEnd) {
//                                 i.onHideLoadingEnd();
//                             }
//                         });
//                     });
//                 });
//             };
//         }
//     };
//     e.prototype.huoqu_tongyong_gongneng = function () {
//         return this.common;
//     };
//     e.prototype.tianjia_zi_youxi = function (e) {
//         this.map[e.name] = e;
//     };
//     e.prototype.huoqu_zi_youxi = function (e) {
//         return this.map[e];
//     };
//     e.prototype.qianwang_youxi = function (e, t, o, a) {
//         var s = this;
//         if (void 0 === o) {
//             o = {};
//         }
//         if (void 0 === a) {
//             a = {};
//         }
//         this.curName = e;
//         var r = this.map[e];
//         if (r.group) {
//             ailin_sdk_manager.initPhysics(r.group);
//         }
//         var c = !this.huoqu_tongyong_gongneng().disableLoading;
//         return (s.common.showLoading && c ? s.common.showLoading() : Promise.resolve()).then(function () {
//             return __awaiter(s, void 0, void 0, function () {
//                 return __generator(this, function (e) {
//                     switch (e.label) {
//                         case 0:
//                             if (a.onShowLoadingEnd) {
//                                 a.onShowLoadingEnd();
//                             }
//                             return r.qianwang_youxi instanceof Promise ? [4, r.qianwang_youxi(t, o)] : [3, 2];
//                         case 1:
//                             e.sent();
//                             return [3, 3];
//                         case 2:
//                             r.qianwang_youxi(t, o);
//                             e.label = 3;
//                         case 3:
//                             if (a.onGoGameEnd) {
//                                 a.onGoGameEnd();
//                             }
//                             return [
//                                 2,
//                                 (s.common.hideLoading && c ? s.common.hideLoading() : Promise.resolve()).then(
//                                     function () {
//                                         if (a.onHideLoadingEnd) {
//                                             a.onHideLoadingEnd();
//                                         }
//                                     }
//                                 )
//                             ];
//                     }
//                 });
//             });
//         });
//     };
//     e.prototype.shezhiYuyan = function (e) {
//         for (var t in this.map) this.map[t].shezhiYuyan(e);
//     };
//     e.prototype.huidao_shouye = function (e) {
//         if (void 0 === e) {
//             e = {};
//         }
//         return this.huoqu_tongyong_gongneng().huidao_shouye(e, e.gameName);
//     };
//     return e;
// })();
// var r = (function () {
//     function e() {}
//     e.huoqu_ziyiuxi_duixiang = function (e) {
//         if (void 0 === e) {
//             e = "default";
//         }
//         if (e in this.map == 0) {
//             this.map[e] = new s();
//         }
//         return this.map[e];
//     };
//     e.tianjia_zi_youxi = function (e, t) {
//         if (void 0 === t) {
//             t = "default";
//         }
//         this.huoqu_ziyiuxi_duixiang(t).tianjia_zi_youxi(e);
//     };
//     e.huoqu_zi_youxi = function (e, t) {
//         if (void 0 === t) {
//             t = "default";
//         }
//         return this.huoqu_ziyiuxi_duixiang(t).huoqu_zi_youxi(e);
//     };
//     e.shezhi_tongyong_gongneng = function (e, t) {
//         if (void 0 === t) {
//             t = "default";
//         }
//         this.huoqu_ziyiuxi_duixiang(t).shezhi_tongyong_gongneng(e);
//     };
//     e.huoqu_tongyong_gongneng = function (e) {
//         if (void 0 === e) {
//             e = "default";
//         }
//         return this.huoqu_ziyiuxi_duixiang(e).huoqu_tongyong_gongneng();
//     };
//     e.qianwang_youxi = function (e, t, o, i, n) {
//         if (void 0 === o) {
//             o = {};
//         }
//         if (void 0 === i) {
//             i = {};
//         }
//         if (void 0 === n) {
//             n = "default";
//         }
//         this.huoqu_ziyiuxi_duixiang(n).qianwang_youxi(e, t, o, i);
//     };
//     e.huidao_shouye = function (e, t) {
//         if (void 0 === e) {
//             e = {};
//         }
//         if (void 0 === t) {
//             t = "default";
//         }
//         return this.huoqu_ziyiuxi_duixiang(t).huidao_shouye(e);
//     };
//     e.map = {};
//     return e;
// })();
// exports.AL_Framework_Sub_Game = r;




exports.AL_Framework_Sub_Game = void 0;

/**
 * @class Common
 * 表示一个通用的功能类
 */
class Common {}

/**
 * @class SubGameManager
 * 管理子游戏的类
 */
class SubGameManager {
    constructor() {
        this.common = new Common();
        this.map = {};
    }

    /** 获取当前游戏名称 */
    huoqu_mingzi() {
        return this.curName;
    }

    /** 设置当前游戏名称 */
    shezhi_mingzi(name) {
        this.curName = name;
    }

    /** 设置通用功能 */
    shezhi_tongyong_gongneng(commonInstance) {
        this.common = commonInstance;
        const goHomeFunc = commonInstance.huidao_shouye;

        if (goHomeFunc) {
            this.common.huidao_shouye = (options = {}, gameName = "") => {
                return (
                    this.common.showLoading
                        ? this.common.disableLoading
                            ? Promise.resolve()
                            : this.common.showLoading()
                        : Promise.resolve()
                ).then(() => {
                    this.common.gameToHomeName = gameName || this.curName;

                    if (options.onShowLoadingEnd) {
                        options.onShowLoadingEnd();
                    }

                    return goHomeFunc(options).then(() => {
                        if (this.common.mainGroup) {
                            ailin_sdk_manager.initPhysics(commonInstance.mainGroup);
                        }

                        if (options.onGoHomeEnd) {
                            options.onGoHomeEnd();
                        }

                        return (
                            this.common.hideLoading
                                ? this.common.disableLoading
                                    ? Promise.resolve()
                                    : this.common.hideLoading()
                                : Promise.resolve()
                        ).then(() => {
                            if (options.onHideLoadingEnd) {
                                options.onHideLoadingEnd();
                            }
                        });
                    });
                });
            };
        }
    }

    /** 获取通用功能实例 */
    huoqu_tongyong_gongneng() {
        return this.common;
    }

    /** 添加子游戏 */
    tianjia_zi_youxi(subGame) {
        this.map[subGame.name] = subGame;
    }

    /** 获取子游戏 */
    huoqu_zi_youxi(subGameName) {
        return this.map[subGameName];
    }

    /** 开始游戏 */
    qianwang_youxi(gameName, params, gameOptions = {}, callbacks = {}) {
        this.curName = gameName;
        const subGame = this.map[gameName];

        if (subGame.group) {
            ailin_sdk_manager.initPhysics(subGame.group);
        }

        const shouldShowLoading = !this.huoqu_tongyong_gongneng().disableLoading;
        return (this.common.showLoading && shouldShowLoading ? this.common.showLoading() : Promise.resolve()).then(
            async () => {
                if (callbacks.onShowLoadingEnd) {
                    callbacks.onShowLoadingEnd();
                }

                if (subGame.qianwang_youxi instanceof Promise) {
                    await subGame.qianwang_youxi(params, gameOptions);
                } else {
                    subGame.qianwang_youxi(params, gameOptions);
                }

                if (callbacks.onGoGameEnd) {
                    callbacks.onGoGameEnd();
                }

                return (
                    this.common.hideLoading && shouldShowLoading ? this.common.hideLoading() : Promise.resolve()
                ).then(() => {
                    if (callbacks.onHideLoadingEnd) {
                        callbacks.onHideLoadingEnd();
                    }
                });
            }
        );
    }

    /** 设置语言 */
    shezhiYuyan(language) {
        for (let key in this.map) {
            this.map[key].shezhiYuyan(language);
        }
    }

    /** 回到首页 */
    huidao_shouye(options = {}) {
        return this.huoqu_tongyong_gongneng().huidao_shouye(options, options.gameName);
    }
}

/**
 * @class AL_Framework_Sub_Game
 * 子游戏管理类，单例
 */
class AL_Framework_Sub_Game {
    static huoqu_ziyiuxi_duixiang(gameName = "default") {
        if (!(gameName in this.map)) {
            this.map[gameName] = new SubGameManager();
        }
        return this.map[gameName];
    }

    static tianjia_zi_youxi(subGame, gameName = "default") {
        this.huoqu_ziyiuxi_duixiang(gameName).tianjia_zi_youxi(subGame);
    }

    static huoqu_zi_youxi(subGameName, gameName = "default") {
        return this.huoqu_ziyiuxi_duixiang(gameName).huoqu_zi_youxi(subGameName);
    }

    static shezhi_tongyong_gongneng(commonInstance, gameName = "default") {
        this.huoqu_ziyiuxi_duixiang(gameName).shezhi_tongyong_gongneng(commonInstance);
    }

    static huoqu_tongyong_gongneng(gameName = "default") {
        return this.huoqu_ziyiuxi_duixiang(gameName).huoqu_tongyong_gongneng();
    }

    static qianwang_youxi(gameName, params, gameOptions = {}, callbacks = {}, subGameName = "default") {
        this.huoqu_ziyiuxi_duixiang(subGameName).qianwang_youxi(gameName, params, gameOptions, callbacks);
    }

    static huidao_shouye(options = {}, gameName = "default") {
        return this.huoqu_ziyiuxi_duixiang(gameName).huidao_shouye(options);
    }
}

AL_Framework_Sub_Game.map = {};
exports.AL_Framework_Sub_Game = AL_Framework_Sub_Game;
